package com.yunzhu.yunzhubookingservice.model.sys;

import com.yunzhu.yunzhubookingservice.exception.CustomException;
import com.yunzhu.yunzhubookingservice.exception.CustomExceptionType;
import lombok.Data;

/**
 * 定义给前端统一结构的响应结果
 */
@Data
public class AjaxResponse {
    private boolean isok; //请求是否成功
    private int code;       //http Status Code
    private String message; //请求失败的提示信息
    private Object data;    //请求成功时响应给前端的数据
    private AjaxResponse(){}

    //请求出现异常时的相依数据封装
    public static AjaxResponse error(CustomException e){
        AjaxResponse resultBean = new AjaxResponse();
        resultBean.setIsok(false);
        resultBean.setCode(e.getCode());
        if(e.getCode() == CustomExceptionType.USER_INPUT_ERROR.getCode()){
            resultBean.setMessage(e.getMessage());
        }else if(e.getCode() == CustomExceptionType.SYSTEM_ERROR.getCode()){
            resultBean.setMessage(e.getMessage()+",系统出现异常，请联系管理处理！tel:88888888");
        }else{
            resultBean.setMessage("系统出现未知异常，请联系管理处理！tel:88888888");
        }
        return resultBean;
    }

    //请求成功时的响应数据封装，没有响应数据（比如删除，修改成功）
    public static AjaxResponse success(){
        AjaxResponse resultBean =new AjaxResponse();
        resultBean.setIsok(true);
        resultBean.setCode(200);
        resultBean.setMessage("success");
        return resultBean;
    }

    //请求成功时响应封装数据，有响应数据（比如查询成功）
    public static AjaxResponse success(Object data){
        AjaxResponse resultBean =new AjaxResponse();
        resultBean.setIsok(true);
        resultBean.setCode(200);
        resultBean.setMessage("success");
        resultBean.setData(data);
        return resultBean;
    }
}
